$(function () {
    var
        _user,											//用户信息
        _query = {
            "index": 0,
            "size": CONFIG_MANAGER.pageSize,
            "query": "",
            "search": {}
        },												//搜索条件·
        _html = [],										//html字符串
        _myScroll,										//滚动
        _thispage,										//当前页
        _pullUp,										//上拉
        _pullDown,										//下拉
        _translateX										//计算偏移值
        ;
    var ordersPage = function () {
        this.init();
    };
    ordersPage.prototype = {
        init: function () {
            this.loadData();
            this.addScroll();
            this.bindEvent();
        },
        addScroll: function () {
            _pullUp = $("#pullUp");
            _pullDown = $("#pullDown");
            _myScroll = new IScroll('#wrapper', {
                probeType: 2,
                scrollbars: true,
                mouseWheel: false,
                fadeScrollbars: true,
                bounce: true,
                interactiveScrollbars: false,
                shrinkScrollbars: 'scale',
                click: true,
                keyBindings: true,
                momentum: true
            });
            _myScroll.on("scrollEnd", function () {
//            	console.log("this.y=" + this.y);
//             	console.log("this.maxScrollY=" + this.maxScrollY);
//				console.log(this);
//				console.dir(this.maxScrollY);
                if ((this.y < 0) && (this.y == this.maxScrollY)) {
                    console.log("滚动到底部");
                    _pullUp.show();
                    setTimeout(function () {
                        _query.index++;
                        _thispage.loadOrders();
                    }, 500);
                } else if ((this.y == 0) && (this.y != this.maxScrollY)) {
                    console.log("滚动到顶部");
                    _pullDown.show();
                    setTimeout(function () {
                        _query.index--;
                        if (_query.index < 0) {
                            _query.index = 0;
                        }
                        ;
                        _thispage.loadOrders();
                    }, 500);
                }
            });
        },
        //加载
        loadData: function () {
            //加载
//          window.addEventListener("load", function () {
//              FastClick.attach(document.body);
//          }, false);

            _user = getLocalStorage(STORAGE_MANAGER.user);
            if (!_user) {
                redirect("../account/login.html");
                return false;
            } else {
                //alert( user.id );
            }
            this.loadOrders();
        },
        loadOrders: function () {
            var requestUrl = CONFIG_MANAGER.RequstUrl + "v1/orders/my/" + _user.id;

            $.ajax({
                type: "POST",
                url: requestUrl,
                data: _query,
                async: true,
                headers: {"token": _user.token},
                beforeSend: function () {
                    $.showWaiting("等待中");
                }
            }).done(function (data) {
                setTimeout(function () {
                    $.closeWaiting();
                }, 500);
                _thispage.loadList(JSON.parse(data));
            });
        },
        //加载列表
        loadList: function (data) {
            var length = data.data.length, list = data.data, _html = [];
            if (0 == length) {
                $("#orderList").html('<div class="zhanwushuju"><img src="../../images/zanwushuju.png"><br>暂无数据</div>');
            } else {
                $.each(list, function (i, order) {
                    _html.push('<div name="divOrder" tip="' + order.id + '" class="bgai mbtopbottom ddnybg">');
                    //订单编号
                    _html.push('<div class="ddnybbt mbbottom">');
                    _html.push('<p class="dfktbdw"><img src="../../images/dfkdw1.png"/></p>');
                    _html.push('<p class="dddpt"><span>订单号：<b>' + order.id + '</b></span>' + order.address + '</p>');
                    _html.push('</div>');
                    //订单商品
                    $.each(order.items, function (j, goods) {
                        _html.push('<div class="mbbottom with100 overflow">');
                        _html.push('<dl class="cpxqxztop cpxqxztop1 mtop0">');
                        _html.push('<dt><img src="../../images/splbtu4.jpg" /></dt>');
                        _html.push('<dd><p class="floatr"><span class="money">￥' + goods.act_price + '</span></p>' + goods.sku_name + '</dd>');
                        _html.push('<dd class="huisezi"><span class="x2">x' + goods.qty + '</span>' + goods.specs + '</dd>')
                        _html.push('</dl>');
                        _html.push('</div>');
                    });
                    //
                    _html.push('<div class="gjjsl mbbottom">');
                    _html.push('<p class="floatr">');
                    _html.push('<span class="jinbi">');
                    _html.push('<b><img src="../../images/jbtub.jpg"></b>' + order.discount_fee + '+');
                    _html.push('</span>');
                    _html.push('<span class="money">￥' + order.pay_fee + '</span>');
                    _html.push('</p>共计' + order.items.length + '件商品(邮费：￥' + order.delivery_fee + ')');
                    _html.push('</div>');
                    //确认
                    _html.push('<p class="ddczan"><span class="huangsebg">去支付</span><span>删除订单</span></p>');

                    _html.push('</div>');


                });
                $("#orderList").html(_html.join(''));
            }
            //加载完成后隐藏上拉下拉提示
            setTimeout(function () {
                _myScroll.refresh();
                _pullUp.hide();
                _pullDown.hide();
            }, 100);
        },
        //页面事件
        bindEvent: function () {

            //点击后退
            $("#back").on("click", function () {
                back();
            });

            //点击遮罩搜索图标，显示遮罩
            $("#search_icon").on("click", function () {
                $("#search_pop").addClass("search_pop_show").removeClass("search_pop_hide");
            });
            //点击遮罩返回，隐藏这招
            $("#hide-pop").on("click", function () {
                $("#search_pop").addClass("search_pop_hide").removeClass("search_pop_show");
            });

            //点击搜索按钮隐藏弹出框
            $("#search_button").on("click", function () {
                _query.query = $("#searchinput").val().replace(/(^\s+)|(\s+$)/g, "");
                if ("" == _query.query) {
                    return;
                }
                _thispage.loadOrders();
                $("#search_pop").addClass("search_pop_hide").removeClass("search_pop_show");
            });

            //点击线上线下切换,更新搜索条件
            $("#online_offline i").on("click", function () {
                $(this).siblings().removeClass("xuanz");
                $(this).addClass("xuanz");
                _query.search.type = $(this).attr('tip');
            });

            //点击订单类型，更新搜索条件
            $("#orderType>li").on("click", function () {
                var tip = $(this).attr("tip"), value = "translateX(" + $(this).offset().left + "px)";
                $("#bottowtab").css({
                    "margin-left": value,
                    "transform": value,
                    "-moz-transform": value,
                    "-webkit-transform": value,
                    "-o-transform": value,
                    "-ms-transform": value
                });
                //搜索条件，订单状态
                switch (tip) {
                    //全部
                    case "all":
                        _query.search.status = "";
                        break;
                    //待付款 包含0-新订单，1-系统确认订单，
                    case "unpay":
                        _query.search.status = 0;
                        break;
                    //待发货  包含2-在线支付完成，5-仓库已接收（商家确认、仓库确认），6-商品打包中 ，7-发货中
                    case "pay":
                        _query.search.status = "";
                        break;
                    //交易完成 包含，10-退款成功，11-退款失败，8-客户已收货
                    case "finish":
                        _query.search.status = "";
                        break;
                    //取消交易 包含3-已取消订单，9-正在退款
                    case "cancel":
                        _query.search.status = "";
                        break;
                    default:
                        break;
                }
                _thispage.loadOrders();
            });

            //点击订单查看订单详情
            $("#orderList").on("click", "div[name='divOrder']", function () {
                var id = $(this).attr("tip");
                redirect("detail.html?id=" + id);
                return false;
            });
        },
    };
    _thispage = new ordersPage();
});